import AlSms, { SendSmsRequest } from '@alicloud/dysmsapi20170525';
import { Config } from '@alicloud/openapi-client';

export interface Create {
  accessKeyId: string;
  accessKeySecret: string;
}
export type ns = number | string;
export interface Send {
  phoneNumbers: ns | ns[];
  content: ns;
  signName: string;
  templateCode: string;
}
export class AL {
  private client: AlSms;
  constructor (param: Create) {
    const config = new Config({
      accessKeyId: param.accessKeyId,
      accessKeySecret: param.accessKeySecret,
      endpoint: "dysmsapi.aliyuncs.com",
    });
    this.client = new AlSms(config);
  }
  async send(param: Send) {
    if (Array.isArray(param.phoneNumbers)) param.phoneNumbers = param.phoneNumbers.join(",");
    else if (typeof param.phoneNumbers == "number") param.phoneNumbers = param.phoneNumbers.toString();
    else if (typeof param.phoneNumbers != "string") return;
    let sendSmsRequest = new SendSmsRequest({
      phoneNumbers: param.phoneNumbers,
      signName: param.signName,
      templateCode: param.templateCode,
      templateParam: JSON.stringify({ code: param.content }),
    });
    return await this.client.sendSms(sendSmsRequest);
  }
}
export default AL;
